package test.date;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DateTest2 {
    public static void main(String[] args) {
        System.out.println(returnNewDate(new Date()));
        System.out.println(myDate(new Date()));
    }

    public static Date returnNewDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        int month = localDate.getMonthValue();
        int[] a = {1, 2, 3};
        int[] b = {4, 5, 6};
        int[] c = {7, 8, 9};
        int[] d = {10, 11, 12};
        int count = 0;//遍历次数（春季）
        for (int i = 0; i < a.length; i++) {
            if (a[i] == month) {
                count = a[0];
            }
        }
        //夏季
        for (int i = 0; i < b.length; i++) {
            if (b[i] == month) {
                count = b[0];
            }
        }
        //秋季
        for (int i = 0; i < c.length; i++) {
            if (c[i] == month) {
                count = c[0];
            }
        }
        //冬季
        for (int i = 0; i < d.length; i++) {
            if (d[i] == month) {
                count = d[0];
            }
        }
        int month2 = count;//首月月份数
        int year = date.getYear();
        LocalDate newDate = LocalDate.of(year, month2, 1);
        ZoneId zoneId2 = ZoneId.systemDefault();
        ZonedDateTime zdt = newDate.atStartOfDay(zoneId2);
        Date newD = Date.from(zdt.toInstant());
        return newD;
    }

    static Date myDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        Date result = new Date();
        c.setTime(result);
        if(month<=2) {
            c.set(Calendar.MONTH, 0);
        } else if(month <=5) {
            c.set(Calendar.MONTH, 3);
        }else if(month <=8) {
            c.set(Calendar.MONTH, 6);
        } else {
            c.set(Calendar.MONTH, 9);
        }
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }
}
